package com.zdy.im.user.dao;


import java.util.Map;

import com.sixeco.framework.core.db.ext.Query;
import com.sixeco.framework.core.mybatis.MyBatisBaseCacheDao;
import com.sixeco.framework.util.PageUtils;
import com.zdy.im.user.mapper.ImUserGroupInfoMapper;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;

import com.zdy.im.user.entity.ImUserGroupInfo;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;




@Service("imUserGroupInfoDao")
public class ImUserGroupInfoDao extends MyBatisBaseCacheDao<ImUserGroupInfoMapper, ImUserGroupInfo> {

    @Autowired
    private ImUserGroupInfoMapper  imUserGroupInfoMapper;

    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ImUserGroupInfo>  iPage =  new Query<ImUserGroupInfo>().getPage(params);

        QueryWrapper<ImUserGroupInfo> query =  new QueryWrapper<>();

        IPage<ImUserGroupInfo> page = this.page(iPage,query );

        return new PageUtils(page);
    }


    @Override
    public void saveData(List<Object> list) {
        List<ImUserGroupInfo> datas = new ArrayList<>();
        list.forEach(e -> datas.add(ImUserGroupInfo.build().clone(e)));
        this.saveBatch(datas, BATCH_SIZE);
        }


    public ImUserGroupInfo selectDefGroupOne(long userId){
        QueryWrapper<ImUserGroupInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId)
                .eq("sort",0)
                .eq("status",1);
        List<ImUserGroupInfo> imUserGroupInfos = this.list(queryWrapper);
        if(CollectionUtils.isEmpty(imUserGroupInfos)){
            return null;
        }
        return imUserGroupInfos.get(0);
    }

    public ImUserGroupInfo selectGroupOneByGroupName(long userId,String groupName){
        QueryWrapper<ImUserGroupInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId)
                .eq("group_name",groupName)
                .eq("status",1);
        List<ImUserGroupInfo> imUserGroupInfos = this.list(queryWrapper);
        if(CollectionUtils.isEmpty(imUserGroupInfos)){
            return null;
        }
        return imUserGroupInfos.get(0);
    }

    /**
     * 查询某个用户，所有组列表
     * @param userId
     * @return
     */
    public List<ImUserGroupInfo> selectListByUserId(long userId){
        QueryWrapper<ImUserGroupInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId)
                .eq("status",1)
                .orderByAsc("sort");
        return this.list(queryWrapper);
    }

    /**
     * 查询某用户，组的最大序号
     * @param userId
     * @return
     */
    public ImUserGroupInfo getOneByUserId(long userId){
        QueryWrapper<ImUserGroupInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId)
                .eq("status",1)
                .orderByDesc("sort");
        List<ImUserGroupInfo> imUserGroupInfos = this.list(queryWrapper);
        if(CollectionUtils.isEmpty(imUserGroupInfos)){
            return null;
        }
        return imUserGroupInfos.get(0);
    }
}